{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Points That Intersect With Cars"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #math #prefix-sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #数学 #前缀和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numberOfPoints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #与车相交的点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>nums</code> 表示汽车停放在数轴上的坐标。对于任意下标 <code>i</code>，<code>nums[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> ，其中 <code>start<sub>i</sub></code> 是第 <code>i</code> 辆车的起点，<code>end<sub>i</sub></code> 是第 <code>i</code> 辆车的终点。</p>\n",
    "\n",
    "<p>返回数轴上被车 <strong>任意部分</strong> 覆盖的整数点的数目。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [[3,6],[1,5],[4,7]]\n",
    "<strong>输出：</strong>7\n",
    "<strong>解释：</strong>从 1 到 7 的所有点都至少与一辆车相交，因此答案为 7 。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [[1,3],[5,8]]\n",
    "<strong>输出：</strong>7\n",
    "<strong>解释：</strong>1、2、3、5、6、7、8 共计 7 个点满足至少与一辆车相交，因此答案为 7 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>nums[i].length == 2</code></li>\n",
    "\t<li><code><font face=\"monospace\">1 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;= 100</font></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [points-that-intersect-with-cars](https://leetcode.cn/problems/points-that-intersect-with-cars/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [points-that-intersect-with-cars](https://leetcode.cn/problems/points-that-intersect-with-cars/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[3,6],[1,5],[4,7]]', '[[1,3],[5,8]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from math import inf\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numberOfPoints(self, nums: List[List[int]]) -> int:\n",
    "        nums.sort()\n",
    "        n_covered: int = 0\n",
    "        point: int = -inf\n",
    "\n",
    "        for start, end in nums:\n",
    "            point = max(point, start)\n",
    "            n_covered += max(0, end - point + 1)\n",
    "            point = max(point, end + 1)\n",
    "\n",
    "        return n_covered\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfPoints(self, nums: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, 101):\n",
    "            for x, y in nums:\n",
    "                if x <= i <= y:\n",
    "                    ans += 1\n",
    "                    break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfPoints(self, nums: List[List[int]]) -> int:\n",
    "        s=set()\n",
    "        for i in nums:\n",
    "            for k in range(i[0],i[1]+1):\n",
    "                s.add(k)\n",
    "        return len(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfPoints(self, nums: List[List[int]]) -> int:\n",
    "        ans=0\n",
    "        for i in range(1,101):\n",
    "            for x,y in nums:\n",
    "                if x<=i<=y:\n",
    "                    ans+=1\n",
    "                    break\n",
    "        return ans      "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
